
<?php
include("modules/IShopStore/pnincludes/LoaderEx.class.php");

function IShopStore_ajax_selector(){
  $ctrl         = FormUtil::getPassedValue ('ctrl', false , 'GET');
  $filter       = FormUtil::getPassedValue ('filter', '1 = 1' , 'GET');
  $name         = FormUtil::getPassedValue ('name', 'selector_' . $ctrl, 'GET');
  $selected     = FormUtil::getPassedValue ('selected', -1234, 'GET');
  $defaultValue = FormUtil::getPassedValue ('defaultValue', 0, 'GET');
  $defaultText  = FormUtil::getPassedValue ('defaultText', '', 'GET');
  $allValue     = FormUtil::getPassedValue ('allValue', 0, 'GET');
  $allText      = FormUtil::getPassedValue ('allText', '', 'GET');
  $idField      = FormUtil::getPassedValue ('idField', 'id', 'GET');
  $nameField    = FormUtil::getPassedValue ('nameField', 'title', 'GET');

  if (($class = LoaderEx::loadClassFromModule ('IShopStore', $ctrl, true)) && $ctrl){
    $objectArray = new $class();
    $objectArray->get ($filter);
    
    echo $objectArray->getSelector (strtolower($name) , $selected, $defaultValue, $defaultText,
                          $allValue, $allText, $idField, $nameField);

    
  }
  return true;
}

/**
* for get ajax data for specify ctrl name
* the template name format "ajax_$func_$ctrl.htm"
*/
function IShopStore_ajax_list() {
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'list' , 'GET');
    //pagnigation variable
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');


    $pagesize = pnModGetVar ('IShopStore', 'pagesize') ? pnModGetVar ('IShopStore', 'pagesize') : 100;
    $render = pnRender::getInstance('IShopStore');
    $render->assign ('_GET', $_GET);
    $render->assign ('_POST', $_POST);
    $render->assign ('_REQUEST', $_REQUEST);

    if ($class = LoaderEx::loadClassFromModule ('IShopStore', $ctrl, true)){
      $objectArray = new $class ();
      $where   = null;
      $sort = null;
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }
      if (method_exists($objectArray,'genSort')){
        $sort = $objectArray->genSort();
      }
      if (method_exists($objectArray,'selectExtendResult')){
        $resultex = $objectArray->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      if (method_exists($objectArray,'genLimit')){
        $pagesize = $objectArray->genLimit();
      }
      if (empty($where)){
        $where = $objectArray->_objWhere;
      }
      if (empty($sort)){
        $sort = $objectArray->_objSort;
      }
      $objectArray->get ($where, $sort,$offset,$pagesize );
      //assign to view
      $render->assign('objectArray', $objectArray->_objData);
    }
    $render->assign('ctrl', $ctrl);
    //$render->assign('setting',getSetting());
    if ($lang){
      $render->assign('lang', $lang);
    }else{
      $render->assign('lang', pnUserGetLang());
    }
    echo $render->fetch(LoaderEx::getClassPath($ctrl) .'ajax_'.$func.'_'.strtolower(LoaderEx::getClassName($ctrl)).'.htm');
    return true;
}

function IShopStore_ajax_view(){
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');
    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'view' , 'GET');
    //$id the id no if edit form
    $id      = FormUtil::getPassedValue ('id', null , 'GET');
    $where   = '';

    $render = pnRender::getInstance('IShopStore');
    $render->assign ('_GET', $_GET);
    $render->assign ('_POST', $_POST);
    $render->assign ('_REQUEST', $_REQUEST);
    
    if ($id){
      //load class
      if (!($class = LoaderEx::loadClassFromModule ('IShopStore', $ctrl, false)))
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");

      $object  = new $class ();
      $object->get($id);
      if (method_exists($object,'selectExtendResult')){
        $resultex = $object->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      $render->assign ('view', $object->_objData);
    }
    $render->assign('ctrl', $ctrl);
    //$render->assign('setting',getSetting());
    if ($lang){
      $render->assign('lang', $lang);
    }else{
      $render->assign('lang', pnUserGetLang());
    }
    echo $render->fetch(LoaderEx::getClassPath($ctrl). 'ajax_'.$func.'_'.strtolower(LoaderEx::getClassName($ctrl)).'.htm');
    return true;
}

/*
* for save setting value with none redirect
* ex module=IShopStore&type=ajax&func=save&ctrl=Setting
*    &form[id]=1&form[value]=myval
*/
function IShopStore_ajax_save(){
  $ctrl =  FormUtil::getPassedValue ('ctrl', null);
  if (!($class = Loader::loadClassFromModule ('IShopStore', $ctrl))){
        return LogUtil::registerError ("Unable to load class [$ctrl] ...");
  }
  $object = new $class ();
  $object->getDataFromInput ('form');
  $object->save ();
  return true;
}

/*
* get setting data 
* @example  in template 
* $setting.MAP_API_KEY
* 
* @return array   the associate key 
*/
function getSetting(){
    $settingArray = DBUtil::selectObjectArray('IShopStore_setting');
    $setting = array();
    foreach($settingArray as $item){
      $setting[$item['key']] = $item['value'];
    }
    unset($settingArray);
    return $setting;
}

/**
* for set/unset status for hot package
* ex. index.php?id=1&module=IShopStore&func=addToCart&type=ajax
*/
function IShopStore_ajax_addToCart() {

  //get status from url 
  $id        = FormUtil::getPassedValue ('id', false , 'REQUEST');
  $ref_id    = FormUtil::getPassedValue ('ref_id', false , 'REQUEST');  
  $name      = FormUtil::getPassedValue ('name', false , 'REQUEST');
  $price     = FormUtil::getPassedValue ('price', false , 'REQUEST');  
  $thumbname = FormUtil::getPassedValue ('thumbname', false , 'REQUEST');    
  
  $preSessionItems = SessionUtil::getVar("_CART");


  if($id){
    if(count($preSessionItems)>0){  
     
      $found = false;
      foreach ($preSessionItems as $key=>$element) {
          if ($key == $id) {
              // update some data
              $preSessionItems[$id]['price']  = $preSessionItems[$id]['price'] + $price;
              $preSessionItems[$id]['amount'] = $preSessionItems[$id]['amount'] + 1;            
              $found = true;
              break;
          }
      }
      //unset($element);
      
      if (!$found) {
          $preSessionItems[$id] = array(
                                        "id"     => $id,
                                        "name"   => $name,
                                        "price"  => $price,
                                        "price_per_item"  => $price,                                      
                                        "amount" => 1 ,
                                        "thumbname" =>  $thumbname                                                    
                                );  
      }    
    }else{
      $preSessionItems[$id] = array(
                      "id"     => $id,
                      "name"   => $name,
                      "price"  => $price,
                      "price_per_item"  => $price,                    
                      "amount" => 1 ,
                      "thumbname" =>  $thumbname            
              );   
    }   
    //print_r($preSessionItems ); exit;
          
  
    SessionUtil::setVar("_CART", $preSessionItems);

  }
  
  $render = pnRender::getInstance('IShopStore');  
  echo $render->fetch('ajax_list_cartside.htm'); 
  pnShutDown();

}


/**
* for set/unset status for hot package
* ex. index.php?id=1&module=IShopStore&func=addToCart&type=ajax
*/
function IShopStore_ajax_getCart() {
  $render = pnRender::getInstance('IShopStore');  
  //$preSessionItems = SessionUtil::getVar("_CART");

  //$render->assign ('cartArray', $preSessionItems); 
    
  echo $render->fetch('ajax_list_cartside.htm'); 
  pnShutDown();

}

function IShopStore_ajax_deleteFromCart() {
  $id    = FormUtil::getPassedValue ('id', false , 'REQUEST');
  $sessionItems = SessionUtil::getVar("_CART");
  //print_r($sessionItems); exit;
  if($sessionItems){
    foreach($sessionItems as $key=>$value){
      if($key == $id){
        // Remove from array
        unset($sessionItems[$id]);       
      }     
    }
    $render = pnRender::getInstance('IShopStore');  
    
    SessionUtil::delVar("_CART");    
    SessionUtil::setVar("_CART", $sessionItems);    
      
    echo $render->fetch('ajax_list_cartside.htm');     
  }else{
    echo "Seesion invalid";
  }  
  pnShutDown();  

}

function IShopStore_ajax_deleteAllCart() {
  SessionUtil::delVar("_CART");
  
  echo "";
  pnShutDown();      
}

/**
* display page with class that extend Object Array
*/
function IShopStore_ajax_listJSON() {

    $ctrl    = FormUtil::getPassedValue ('ctrl', 'Item' , 'GET');
    $func  = FormUtil::getPassedValue ('func', 'json' , 'GET');
    $method  = FormUtil::getPassedValue ('method', '' , 'GET');
    $categories_id = FormUtil::getPassedValue ('categories_id', '' , 'GET');

    $filter   = FormUtil::getPassedValue ('filter', 0, 'GET');
    $offset   = FormUtil::getPassedValue ('start', 0, 'GET');
    $sort  = FormUtil::getPassedValue ('order', false);
    $pagesize = FormUtil::getPassedValue ('pagesize', 4, 'GET');    

    $render = pnRender::getInstance('IShopStore');
    if (!($class = Loader::loadClassFromModule ('IShopStore',$ctrl, true)))
      return LogUtil::registerError ("Unable to load class [$ctrl] ...");  

    $objectArray = new $class ();
    $where   = null;
    $sort = null;
    if (method_exists($objectArray,'genFilter')){
      $where = $objectArray->genFilter();
    }
    if (method_exists($objectArray,'genSort')){
      $sort = $objectArray->genSort();
    }
    if (method_exists($objectArray,'selectExtendResult')){
      $resultex = $objectArray->selectExtendResult();
      $render->assign('extendResult', $resultex);
    }
    if (empty($where)){
      $where = implode(' AND ',$objectArray->_objWhere);
    }else if (is_array($objectArray->_objWhere)){
      $where .= implode(' AND ',$objectArray->_objWhere);
    }else if (!empty($objectArray->_objWhere)){
      $where .= ' AND ' . $objectArray->_objWhere;
    }
    if (empty($sort)){
      $sort = $objectArray->_objSort;
    }

    //Spilt page
    $pager = array();
    $pager['numitems']     = $objectArray->getCount($where , true);
    $pager['itemsperpage'] = $pagesize;
    $render->assign ('startnum', $offset);
    $render->assign ('pager', $pager);
    $objectArray->get ($where, $sort , $offset, $pagesize);
   
    echo json_encode($objectArray->_objData);
        
    pnShutDown();

}


/**
* for set/unset status for hot package
* ex. index.php?id=1&module=IShopStore&func=addToCart&type=ajax
*/
function IShopStore_ajax_listCart() {
  $render = pnRender::getInstance('IShopStore');  
  echo $render->fetch('ajax_list_cart.htm'); 
  pnShutDown();
}



/**
* for set/unset status for hot package
* ex. index.php?id=1&module=IShopStore&func=addToCart&type=ajax
*/
function IShopStore_ajax_listPopUpCart() {
  $render = pnRender::getInstance('IShopStore');  
  echo $render->fetch('ajax_popup_cart.htm'); 
  pnShutDown();
}